﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace crdmcgui
{
    public class Template :IEquatable<Template>
    {
        public string Name{get; set;}
        public ParameterType Type {get; private set;}
        public SortableBindingList<crdmcgui.DataManagerParameters> Items { get; set; }

        public override string ToString()
        {
            string strReturn = "";
            for (int i = 0; i < Items.Count; i++)
            {
                strReturn += Items[i].Compile();
                if (i < Items.Count - 1) strReturn += ",";
            }
            return strReturn;
        }

        private Template()
        {
            this.Name = "GenericTemplate";
            this.Items = new SortableBindingList<DataManagerParameters>();
        }

        public Template(string strParameters, ParameterType itemType)
            :this(itemType)
        {
            Parse(strParameters);
        }

        public Template(string strName, ParameterType itemType, string strParameters)
            : this(strParameters, itemType)
        {
            this.Name = strName;
        }

        public Template(ParameterType itemType)
            :this()
        {
            this.Type = itemType;
        }

        public void Parse(string strTemplate)
        {
            string[] tmpRules = strTemplate.Trim().Split(new string[] { ">>,<<", "<<",">>" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < tmpRules.Length; i++)
            {
                crdmcgui.DataManagerParameters tmpRule = new DataManagerParameters(this.Type,tmpRules[i]);
                this.Items.Add(tmpRule);
            }
        }

        public bool Equals(Template other)
        {
            return this.Type == other.Type && this.Name == other.Name;
        }
    }
}
